﻿Public Class ProjektServiceImpl
    Inherits PersistenceServiceImpl(Of Projekt, ProjektManager)
    Implements ProjektService

    Sub New()
        MyBase.New(New ProjektManagerImpl())
    End Sub

    Protected Overrides Sub beforeDelete(ByRef entity As Projekt)
        Context.TASK_SERVICE.deleteByProjekt(entity)
    End Sub

    Protected Overrides Sub beforeSave(ByRef entity As Projekt)
        For Each task As Task In entity.tasks
            Context.TASK_SERVICE.save(task)
        Next
    End Sub

    Protected Overrides Sub fillUp(ByRef entity As Projekt)
        entity.tasks = Context.TASK_SERVICE.findByProjekt(entity)
        For Each task As Task In entity.tasks
            task.projekt = entity
        Next
    End Sub

    Public Sub deleteByKunde(ByRef kunde As Kunde) Implements ProjektService.deleteByKunde
        addFireBlock()
        Connection.open()
        For Each projekt As Projekt In kunde.projekte
            beforeDelete(projekt)
        Next
        manager.deleteByKunde(kunde)
        Connection.close()
        ' löschen benachrichtigen
        removeFireBlock()
        For Each projekt As Projekt In kunde.projekte
            fireDeleted(projekt)
        Next
    End Sub

    Public Function findByKunde(ByRef kunde As Kunde) As System.Collections.Generic.List(Of Projekt) Implements ProjektService.findByKunde
        Connection.open()
        Dim results As List(Of Projekt) = manager.findByKunde(kunde)
        For Each projekt As Projekt In results
            fillUp(projekt)
        Next
        Connection.close()
        Return results
    End Function
End Class
